package com.regex.web.controller.community;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.regex.web.cache.DictCacheService;
import com.regex.web.entity.community.CommunityTypeDTO;
import com.regex.web.service.community.ICommunityTypeService;
import com.regex.web.utils.AjaxUtil;
import com.regex.web.utils.Assist;
import com.regex.web.utils.Contant;
import com.regex.web.utils.QueryParam;
import com.regex.web.utils.QueryResult;

/**
 * 
 * 〈一句话功能简述〉<br> 
 * 社区类型管理
 *
 * @author admin
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
@EnableAutoConfiguration
@RequestMapping("sys/communityType")
public class SysCommunityTypeController {

    @Autowired
    private DictCacheService memCacheService;
    
    @Autowired
    private ICommunityTypeService communityTypeService;
    
    @RequiresPermissions("communityType:show")
    @RequestMapping("show")
    public String show(QueryParam param, Model model) {
        
        Assist assist = new Assist();
        assist.setRequires(Assist.andEq("is_del", "0"));
        if(param.getPageSize() <= 0) {
            param.setPageSize(Contant.NONMAL_PAGE_SIZE);
        }
        assist.setRowSize(param.getPageSize());
        if(param.getPageNumber() < 1) {
            param.setPageNumber(1);
        }
        
        Page<CommunityTypeDTO> page = new Page<CommunityTypeDTO>();
        page.setSize(param.getPageSize());
        page.setCurrent(param.getPageNumber());
        QueryWrapper<CommunityTypeDTO> param1 = new QueryWrapper<CommunityTypeDTO>();
        param1.orderByDesc("sort ASC, create_time");
        QueryResult<CommunityTypeDTO> result = communityTypeService.page(page, param1);
        model.addAttribute("result", result);
        
        Map<String, Object> dictMap = memCacheService.getDictCache();
        model.addAttribute("dictMap", dictMap);
        return "sys/communityType/show";
    }
    
    
    /**
     * 
     * 功能描述: <br>
     * 详情
     *
     * @param id
     * @param model
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequiresPermissions("communityType:show")
    @RequestMapping("detial")
    public String detial(Long id, Model model) {
        try {
            CommunityTypeDTO CommunityTypeDTO = communityTypeService.getById(id);
            model.addAttribute("CommunityTypeDTO", CommunityTypeDTO);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "sys/communityType/detial";
    }
    
    /**
     * 
     * 功能描述: <br>
     * 删除
     *
     * @param id
     * @param response
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequiresPermissions("communityType:del")
    @RequestMapping("del")
    public void del(Long id, HttpServletResponse response) {
        int n = 0;
        if(id>6) {
            boolean flag = communityTypeService.removeById(id);
            if(flag) {
                n = 1;
            }
        } else {
            n = 2;
        }
        AjaxUtil.ajaxJsonSucMessage(response, n);
    }
    
    /**
     * 
     * 功能描述: <br>
     * 修改/ 新增
     *
     * @param id
     * @param model
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequiresPermissions("communityType:edit")
    @RequestMapping("input")
    public String input(Long id, Model model) {
        int sort = 10;
        
        if(id!= null && id > 0) {
            CommunityTypeDTO CommunityTypeDTO = communityTypeService.getById(id);
            sort = CommunityTypeDTO.getSort();
            model.addAttribute("CommunityTypeDTO", CommunityTypeDTO);
        } else {
            int count = (int) communityTypeService.count();
            sort = (count + 1) * 10;
        }
        model.addAttribute("sort", sort);
        
        Map<String, Object> dictMap = memCacheService.getDictCache();
        Map<String, String> communityTypeMap = (Map<String, String>) dictMap.get("community_type");
        model.addAttribute("communityTypeMap", communityTypeMap);
        return "sys/communityType/input";
    }
    
    /**
     * 
     * 功能描述: <br>
     * 保存
     *
     * @param CommunityTypeDTO
     * @param response
     * @param request
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequiresPermissions("communityType:edit")
    @RequestMapping("save")
    public void save(CommunityTypeDTO CommunityTypeDTO, HttpServletResponse response,
            HttpServletRequest request) {
        int n = 0;
        try {
        if(StringUtils.isNotEmpty(CommunityTypeDTO.getName())
                && StringUtils.isNotEmpty(CommunityTypeDTO.getIconUrl())
                && StringUtils.isNotEmpty(CommunityTypeDTO.getType())) {
            CommunityTypeDTO.setUpdateTime(new Date());
            if(CommunityTypeDTO.getId() > 0) {
                boolean flag = communityTypeService.updateById(CommunityTypeDTO);
                if(flag) {
                    n =1;
                }
            } else {
                CommunityTypeDTO.setIsDel("0");
                CommunityTypeDTO.setCreateTime(new Date());
                boolean flag = communityTypeService.save(CommunityTypeDTO);
                if(flag) {
                    n =1;
                }
            }
        } else {
            n = 2;
        }
        } catch(Exception e) {
            e.printStackTrace();
            n = 3;
        }
        AjaxUtil.ajaxJsonSucMessage(response, n);
    }



}
